import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
const loader = new GLTFLoader();
const model = new THREE.Group();
loader.load('/工厂.glb',(gltf)=>{
    model.add(gltf.scene);
})
loader.load('/无人机.glb',(gltf)=>{
    const fly = new THREE.Group();
    fly.add(gltf.scene)
    const axesHelper = new THREE.AxesHelper(15);
    fly.add(axesHelper)

    const R = 120
    const H = 100
    fly.position.x = R
    fly.position.y = H
    model.add(fly);
    let angle = 0
    const q0 = fly.quaternion.clone()
    const a = new THREE.Vector3(0,0,1)
    const target = new THREE.Vector3(0,H,0)
    const loop = () => {
        angle = angle + 0.01
        fly.position.x = R * Math.cos(angle)
        fly.position.z = R * Math.sin(angle)
        // 无人机飞行时指向的方向
        const b = target.clone().sub(fly.position).normalize()
        const q = new THREE.Quaternion()
        q.setFromUnitVectors(a,b)
        const newQ = q0.clone().multiply(q)
        fly.quaternion.copy(newQ)
        // 浏览器用于定时循环操作某一个函数，一秒钟60次左右
        window.requestAnimationFrame(loop);
      };
      
      loop();
})


export default {model};